package com.cc.mall.config;

import com.cc.mall.entity.ProductDetailEntity;
import com.cc.mall.entity.ProductEntity;
import com.cc.mall.service.ProductDetailService;
import com.cc.mall.service.ProductService;
import com.cc.mall.vo.IdAndNumVo;
import com.cc.mall.vo.Kucun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.List;

@Configuration
@EnableScheduling
public class ScheduleTask {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductDetailService productDetailService;

    //3.添加定时任务
    @Scheduled(cron = "0 0 * * * ?")
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    private void configureTasks() {
        /*// 线上
        List<Kucun> onlineKucun = productDetailService.getOnlineKucun();
        // 线下
        List<Kucun> offlineKucun = productDetailService.getOfflineKucun();

        for (Kucun onkucun : onlineKucun) {

            for (Kucun offkucun : offlineKucun) {
                if(onkucun.getSkuId().equals(offkucun.getSkuId())){
                    onkucun.setProductNumber(onkucun.getProductNumber()+offkucun.getProductNumber());
                }
            }
            ProductDetailEntity byId = productDetailService.getById(onkucun.getSkuId());
            if(byId.getProductRemainNumber().compareTo(onkucun.getProductNumber()) <= 0){
                byId.setProductRemainNumber(0L);
                productDetailService.updateById(byId);
            }
        }*/
        List<IdAndNumVo> idAndNumVos = productService.getSpuIdAndNumList();
        System.out.println(idAndNumVos);
        for (IdAndNumVo idAndNumVo : idAndNumVos) {
            if(idAndNumVo.getNum() != null){
                if(idAndNumVo.getNum().compareTo(0L) <= 0){
                    ProductEntity productEntity = new ProductEntity();
                    productEntity.setId(idAndNumVo.getSpuId());
                    productEntity.setPublishStatus(2);
                    productService.updateById(productEntity);
                }
            }

        }
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
    }
}
